from khronos.utils.parsing.lexer import Token, DiscardToken, Lexer
from khronos.utils.parsing.bnf import BNF

class digits(Token):
    regex = r"[1-9]\d*"
    
class decimal(Token):
    regex = digits.regex + r"\.\d*"
    
class newline(Token):
    regex = r"\n"
    
class identifier(Token):
    regex = r"[a-zA-Z_][a-zA-Z_0-9]*"
    keywords = ["if", "while", "for"]
    
    def __init__(self, ident):
        type = ident if ident in identifier.keywords else None
        Token.__init__(self, ident, type)
        
class comment(DiscardToken):
    regex = r"#.*"
    
p = BNF()
l = Lexer([digits, decimal, newline, identifier, comment], literals="+-*/")
t = """#23894729837429 4 5 6 3.14 7sjdjf fje_34 4_43ui binho for me  while not  drunk if available
if * for/while
"""
l.init(t)

# -----------------------------------------------
class integer(NonTerminal):
    @production("['+'] digits")
    def positive(self, left, right):
        left.value = int(elems[1])
        
    @production("'-' digits")
    def negative(self, elems):
        left.value = -int(elems[1])
        
p = Parser()

class ProductionMeta(type):
    def __new__(cls, name, bases, dct):
        return type.__new__(cls, name, bases, dct)
        
    def __init__(cls, name, bases, dct):
        super(ProductionMeta, cls).__init__(name, bases, dct)
        
class Production(object):
    __metaclass__ = ProductionMeta
    